
'use strict';


import {Users} from './users/users.js';
import {Transactions} from './transaction/transactions.js';
import {wsRest} from '../../server/hfc/ws_hfc.js';
let faker = require('faker/locale/zh_CN');

class tradeHandler{
}

export const TradeOp = new tradeHandler();

//申请项目
TradeOp.applyOp=function(fromuserid,touserid,tag,data){
    //获取用户信息
	var fu = Users.findOne({userId:fromuserid});
    var tu = Users.findOne({userId:touserid});
    //创建交易数据
    let tid1 = faker.finance.bitcoinAddress();
    let trans = {
        transactionId:tid1,
        tags:tag,
        title:data.projectname,
        createdAt: new Date(),
        avatarFrom: fu.avatar,
        titleFrom: fu.title,
        userFrom:fu.userId,
        avatarTo: tu.avatar,
        titleTo: tu.title,
        userTo:tu.userId,
        desc:data.t_desc,
        data:data
    };
    var hfccontent = "";
    switch(tag){
            case '申请项目': 
                hfccontent = fu.title+" to "+tu.title+" 申请项目，项目名称="+data.projectname;
            break;
            case '申请实施': 
                hfccontent = fu.title+" to "+tu.title+" 申请实施项目，项目名称="+data.projectname;
            break;
            case '申请拨款': 
                hfccontent = fu.title+" to "+tu.title+" 申请项目拨款，项目名称="+data.projectname;
            break;
            case '申请报账': 
                hfccontent = fu.title+" to "+tu.title+" 申请项目报账，项目名称="+data.projectname;
            break;
    }
    
    Transactions.insert(trans);
    
    var hfctrans = {
        tradecustomid:fu.userId,
        content:hfccontent
    }
    wsRest.perform_trade_invoke4Sync(hfctrans);
}

//申请项目
TradeOp.confirmTrade=function(fid,tid,paynumber){
    console.log("--------------------------------------output="+paynumber);
    //载入待确认交易
    let t0 = Transactions.findOne({transactionId:tid});
    let uf = Users.findOne({userId:fid});
    let ut = Users.findOne({userId:t0.userFrom});

    let dt_confirm = new Date();
    //let tid1 = faker.finance.bitcoinAddress();
   
  
    //修改目标交易确认时间
    Transactions.update({transactionId:tid}, {
      $set: { confirmAt: dt_confirm },
    });

    var hfccontent = "";
    switch(t0.tags){
            case '申请项目': 
                hfccontent = uf.title+" to "+ut.title+" 同意申请项目，项目名称="+t0.data.projectname;
            break;
            case '申请实施': 
                hfccontent = uf.title+" to "+ut.title+" 同意申请实施项目，项目名称="+t0.data.projectname;
            break;
            case '申请拨款': 
                hfccontent = uf.title+" to "+ut.title+" 同意项目拨款，项目名称="+t0.data.projectname;
            break;
            case '申请报账': 
                hfccontent = uf.title+" to "+ut.title+" 同意项目报账，项目名称="+t0.data.projectname;
            break;
            case '财政拨款': 
                hfccontent = uf.title+" to "+ut.title+" 确认项目拨款，已通知银行转账，金额="+paynumber+"，项目名称="+t0.data.projectname;
            break;
            case '已转账到村民通知': 
                hfccontent = uf.title+" to "+ut.title+" 收到财政转账给村民通知，转账金额="+t0.paynumber+"，项目名称="+t0.data.projectname;
            break;
            case '村民接收转账': 
                hfccontent = uf.title+" to "+ut.title+" 确认财政转账，金额="+t0.paynumber+"，项目名称="+t0.data.projectname;
            break;
    }
    
    var hfctrans = {
        tradecustomid:uf.userId,
        content:hfccontent
    }
    wsRest.perform_trade_invoke4Sync(hfctrans);

    if(t0.tags == "申请项目"){
        var tdata = t0.data;
        var pinfo = {
                    "title" : tdata.title,
                    "createdAt" : new Date(),
                    "userId" : tdata.userId,
                    "avatar" : "/uifaces/faces/twitter/theonlyzeke/jinzhunfp.jpg",
                    "status" : "已认证",
                    "utype"  : "项目",
                    "desc"   : tdata.desc,
                    "createdBy" : t0.userFrom,
                    "balance" : 0
                };
        let query={"userId":pinfo.userId};
        let fus=[];
        console.log("entry");
        fus = Users.find(query).fetch();
        console.log("entry1");
        if(fus.length <= 0){
          console.log("entry2");
          Users.insert(pinfo);
        }        
    } else if(t0.tags == "申请拨款"){
        //创建交易数据
        let tid5 = faker.finance.bitcoinAddress();
        let trans1 = {
            transactionId:tid5,
            tags:'财政拨款',
            title:t0.data.projectname,
            createdAt: new Date(),
            avatarFrom: uf.avatar,
            titleFrom: uf.title,
            userFrom:uf.userId,
            avatarTo: '/uifaces/faces/twitter/_ragzor/128.jpg',
            titleTo: '县财政局',
            userTo:'caizhengju',
            desc:t0.data.t_desc,
            data:t0.data
        };
        Transactions.insert(trans1);
        let hfccontent1 = uf.title+" to "+"县财政局"+" 请按照申请数额拨款，项目名称="+t0.data.projectname;
        var hfctrans1 = {
            tradecustomid:uf.userId,
            content:hfccontent1
        }
        wsRest.perform_trade_invoke4Sync(hfctrans1);
    }else if(t0.tags == "财政拨款"){
        //创建交易数据
        let tid7 = faker.finance.bitcoinAddress();
        let trans3 = {
            transactionId:tid7,
            tags:'已转账到村民通知',
            title:t0.data.projectname,
            createdAt: new Date(),
            avatarFrom: uf.avatar,
            titleFrom: uf.title,
            userFrom:uf.userId,
            avatarTo: '/uifaces/faces/twitter/santi_urso/128.jpg',
            titleTo: '乡扶贫办',
            userTo:'xiang',
            desc:'已转账到村民通知，财政已经转账给村民，金额='+paynumber,
            "paynumber":paynumber,
            data:t0.data
        };
        Transactions.insert(trans3);
        let hfccontent3 = uf.title+" to "+"乡扶贫办"+" 财政已经转账给村民,金额="+paynumber+"项目名称="+t0.data.projectname;
        var hfctrans3 = {
            tradecustomid:uf.userId,
            content:hfccontent3
        }
        wsRest.perform_trade_invoke4Sync(hfctrans3);



        //创建交易数据
        let tid8 = faker.finance.bitcoinAddress();
        let trans4 = {
            transactionId:tid8,
            tags:'村民接收转账',
            title:t0.data.projectname,
            createdAt: new Date(),
            avatarFrom: uf.avatar,
            titleFrom: uf.title,
            userFrom:uf.userId,
            avatarTo: '/uifaces/faces/twitter/akmur/128.jpg',
            titleTo: '村民',
            userTo:'cunmin',
            desc:'财政局转账，请接收转账，金额='+paynumber,
            paynumber:paynumber,
            data:t0.data
        };
        Transactions.insert(trans4);
        let hfccontent4 = uf.title+" to "+"村民"+" 请确认财政局转账，金额="+paynumber+"项目名称="+t0.data.projectname;
        var hfctrans4 = {
            tradecustomid:uf.userId,
            content:hfccontent4
        }
        wsRest.perform_trade_invoke4Sync(hfctrans4);

    }




}



